package com.chris.cc.user.controller;

import com.chris.cc.common.web.R;
import com.chris.cc.user.model.dto.PermissionDto;
import com.chris.cc.user.service.wrapper.PermissionServiceWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright (c) 2018-2025, chris All rights reserved.
 * <p>
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * <p>
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: haoka (haokang207@126.com)
 * Date: 2018/8/15 14:09
 * 权限
 **/
@RestController
@RequestMapping("/permission")
@Slf4j
public class PermissionController {

    @Autowired
    private PermissionServiceWrapper permissionServiceWrapper;

    @GetMapping("tree")
    public R tree() {
        return R.builder().data(permissionServiceWrapper.getPermissionTree()).build();
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.builder().data(permissionServiceWrapper.getPermissionById(id)).build();
    }

    @PostMapping
    public R add(@RequestBody PermissionDto permission) {
        try {
            permissionServiceWrapper.addPermission(permission);
            return R.builder().build();
        } catch (Exception e) {
            log.error("add permission", e);
            return R.builder().ret(1).msg(e.getMessage()).build();
        }
    }

    @PutMapping
    public R update(@RequestBody PermissionDto permission) {
        try {
            permissionServiceWrapper.updatePermission(permission);
            return R.builder().build();
        } catch (Exception e) {
            log.error("update permission", e);
            return R.builder().ret(1).msg(e.getMessage()).build();
        }
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Long id) {
        try {
            permissionServiceWrapper.deletePermission(id);
            return R.builder().build();
        } catch (Exception e) {
            log.error("delete", e);
            return R.builder().ret(1).msg(e.getMessage()).build();
        }
    }

    @GetMapping("/get/{userId}")
    public R getByUserId(@PathVariable("userId") Long userId) {
        return R.builder().data(permissionServiceWrapper.getPermissionByUserId(userId)).build();
    }

    @GetMapping("/role/{roleId}")
    public R getByRoleId(@PathVariable("roleId") Long roleId) {
        return R.builder().data(permissionServiceWrapper.getPermissionByRoleId(roleId)).build();
    }

    @GetMapping
    public R query(Long parentId, Boolean enable, String keyword, int page, int limit, boolean isAsc) {
        PageRequest pageRequest = PageRequest.of(page - 1, limit, Sort.by(isAsc ? Sort.Direction.ASC : Sort.Direction.DESC,
                "name"));
        return R.builder().data(permissionServiceWrapper.queryPermission(parentId, enable, keyword, pageRequest)).build();
    }

    @PutMapping("/role/{roleId}")
    public R updateRolePermission(@PathVariable("roleId") Long roleId, @RequestBody Long[] permissionIds) {
        permissionServiceWrapper.updateRolePermission(roleId, permissionIds);
        return R.builder().build();
    }
}
